<?php
namespace app\api\controller\yluser;

class Ordermtom extends Base{

    public function __construct() {
        parent::__construct();
        $this->checkLogin();
    }

    //收益记录
    public function lists(){
        $d = $this->post();
        $rule = [
        ];
        $msg = [
        ];
        check($rule,$msg,$d);

        $map = [];
        $map[] = ["and","user_id","=",$this->userinfo["id"]];
        $map[] = ["and","money_type","=","money_sy"];
        if($d["status"] > 0){
            $map[] = ["and","show_money",">",0];
        }else if($d["status"] < 0){
            $map[] = ["and","show_money","<",0];
        }

        $w2 = where($map);

        //找本月
        $month_time = month_time($d['ym']);
        $map[] = ["and","create_time",">=",$month_time['start_time']];
        $map[] = ["and","create_time","<=",$month_time['end_time']];
        
        $w = where($map);
        $order = "order by id desc";
        $sql = "select id,order_no,money,create_time,type,withdraw_status,show_money from user_bill $w[0] {$order} ";
        $list = $this->db->query($sql,$w[1]);

        if(!$list){
            //如果没有找到
            //找 <$end_time 的第一条
            $rs = $this->db->query("select id,order_no,money,create_time,type,withdraw_status,show_money from user_bill $w2[0] and create_time<{$month_time['start_time']}",$w2[1],1);
            if($rs){
                $month_time = month_time(date("Y-m",$rs['create_time']));
                $sql = "select id,order_no,money,create_time,type,withdraw_status,show_money from user_bill $w2[0] and create_time >= {$month_time['start_time']} and create_time <= {$month_time['end_time']} {$order} ";
                $list = $this->db->query($sql,$w2[1]);
            }
        }

        foreach($list as $k => $v){
            $list[$k]['type_name'] = c("type.user_bill_type.{$v['type']}");
            $list[$k]['withdraw_status_name'] = '';
            if (in_array($v['withdraw_status'],[0,1,2])) {
                $list[$k]['withdraw_status_name'] = c("type.withdraw_status.{$v['withdraw_status']}");
            }            
            $list[$k]['create_time_name'] = com_time_format($v['create_time']);
        }
        suc(['list'=>$list,'ym'=>date("Y-m",strtotime('-1 month',strtotime(date('Ym01',$month_time['start_time']))))]);

    }

    //收益转出余额
    public function sytoye(){
        $d = $this->post;
        $rule = [
            'money'=>'require|gt:0',
        ];
        $msg = [
            'money.require' => '请输入转出金额',
            'money.gt' => '转出金额不能小于0',
        ];
        check($rule,$msg,$d);

        $rs = s("Ordermtom",'c')->sytoye($this->userinfo['id'],$d['money']);
        if ($rs) {
            suc("转出成功");
        }else{
           err(s("Ordermtom",'c')->getError());
        }
    }

}
?>
